package com.wushijia.highconcurrency.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTest {

  public static void main(String[] args) {
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    new Thread(() -> {
      lock.lock();
      try {
        System.out.println("begin wait");
        condition.await();//插入到条件变量的阻塞队列队尾，并阻塞挂起该线程，需要另外一个线程唤醒
        System.out.println("end wait");
      } catch (InterruptedException e) {
        e.printStackTrace();
      } finally {
        lock.unlock();
      }
    }).start();

    new Thread(() -> {
      lock.lock();
      try {
        System.out.println("begin signal");
        condition.signal();//唤醒条件变量队列中队头的节点线程放到AQS队列中
        System.out.println("end signal");
      } catch (IllegalMonitorStateException e) {
        e.printStackTrace();
      } finally {
        lock.unlock();
      }
    }).start();

  }
}
